var username=localStorage.getItem("username");
var token=localStorage.getItem("token");
var userid=localStorage.getItem("userid");
var id = localStorage.getItem("id");
var areaId = localStorage.getItem("areaId");
$(function(){
	$.ajax({
		type : 'GET',
		contentType : 'application/json',
		async : false,
		data : {
			id : id,
			areaId:areaId,
			token:token
		},
		url : server_url+'/rest/supplyManage/getSupplyAreaById.do?ran='+Math.random(),
		dataType : 'json',
		success : function(data) {
			if (data.retCode == "0000") {
				console.log(data.data);
				pasteData(data.data);
				} else{
				easyDialog.open({
					container:{
						header:'温馨提示',
						content:data.retMsg
					},
					autoClose : 2000
				});
			}
		}
	});
	
	$("#provinceId").change(function(){
		areaParentChange("cityId", $(this).val(),"countyId");
	});
	$("#cityId").change(function(){
		areaParentChange("countyId", $(this).val());
	});
	/*$("#accountProvince").change(function(){
		areaParentChange("accountCity", $(this).val());
	});*/
	
	
});

function isEmpty(data){
	if(data==null||data==undefined||data.length==0){
		return true;
	}else{
		return false;
	}
}
function pasteData(data){
	var supply = data.adminAreaVo;
	//console.log(supply[0].province);
	var provinces = data.provinces;
	var citys = data.citys;
	//console.log(citys);
	var countrys = data.countrys;
	//$("#supplyName").text(supply[0].supplyName);	
	mapIfExist(function(province){
		$("<option value="+province.areaId+">"+province.areaName+"</option>").appendTo("#provinceId");
	},provinces);
	mapIfExist(function(city){
			$("<option value="+city.areaId+">"+city.areaName+"</option>").appendTo("#cityId");
		},citys);
	mapIfExist(function(country){
		$("<option value="+country.areaId+">"+country.areaName+"</option>").appendTo("#countyId");
	},countrys);
	$("#provinceId").val(supply.province);
	$("#cityId").val(supply.city);
	$("#countyId").val(supply.county);
};

//返回
function toBack(){
	localStorage.removeItem("id");
	localStorage.removeItem("areaId");
	var url=server_url+'/supplyManage/areaList.do';
	//tl_config.last_url=url;
	tl_config.loadToWorkArea(url);
}
//添加列表行
function addSupplyArea(key,value){
	if(key == undefined || key == null){
		key = "";
	}
	if(value == undefined || value == null){
		value = "";
	}
	// 表示现在到第几行
	var index = $("#fromProductSpec [name=specName]").size();
			$("#supply_table").append(""+
					'<ul class="l_staffxq3 clearfix" id="keyValue'+index+'Tr">' +
						' <li class="clearfix">'+
							'<span class="l_left">区域</span>'+
							'<div class="l_right" style="width: 898px;display: table-cell;">'+
								'<div class="l_txstaff" style="height: 50px; margin:0px " id="areaNameId">'+
									'<select name="province" id="provinceId" style="margin-top:13px" onclick="queryProvince();"><option>省/市</option></select>'+
			     				    '<select name="city" id="cityId"><option>县/市辖区</option></select>'+
			     				    '<select name="county" id="countyId"><option>县/区</option></select>'+	   
		     				    '</div>'+
		     				   '<span><a href="javascript:delRow('+index+');" class="z_delpost">删除</a></span>'+
		     				'</div>'+	
		     			'</li>'+
		     		'</ul>'  				    			    
		);		
}
//删除列表行
function delRow(index) {
	$("#keyValue"+index+"Tr").remove();
}
function initSupplyData(areaId,data,i){
	$.ajax({
		url:server_url+"/rest/supplyManage/getAreaName.do?areaId="+areaId,
		type:"get",
		async:false,
		success:function(datas){
			//console.log(datas);
			if(datas.retCode=="0000"){
				appendHtml(datas.data,data,i);
			}else{
				easyDialog.open({
					  container : {
						  header : '提示',
					      content : data.retMsg
					  },
					  autoClose : 2000
					});	
			}
		}
	})
};

//添加
function addSupplyArea(){
	var url="supplyManage/supplyAreaAdd.do?ran="+Math.random();
	tl_config.loadToWorkArea(url);
};
function replaceEmptyItem(arr){
    for(var i=0,len=arr.length;i<len;i++){
        if(!arr[i]|| arr[i]==''){
            arr.splice(i,1);
            len--;
             
        }
    }
};
function submitCommit(){
	$.ajax({
		url :server_url+'/rest/supplyManage/getSupplyAreaById.do?ran='+Math.random(),
		type:"post",
		data:{
			id:id,
			areaId:areaId
			},
		success:function(data){
			var areaIds = data.data.supplyArea.data.areaId;
			var Id = areaIds.split(",");
			replaceEmptyItem(Id);
			//console.log(Id.length);
			for(var i = 0;i<Id.length;i++){
				if(Id[i]==areaId){
					//Id[i] = $("#countyId").val()+",";
					Id = areaIds.replace(Id[i],$("#countyId").val());
					//console.log(Id);
					//console.log($("#countyId").val());
					if(Id.length>1){
						if(Id[i]==$("#countyId").val()){
							easyDialog.open({container:{header:'温馨提示',content:"该区域已经存在！请重新输入"},autoClose : 2000});
							return;
						}else{
							$.ajax({
								type:"get",
								data:{
									id:id,
									areaId:Id
								},
								url:server_url+'/rest/supplyManage/updateById.do?token='+token+'&ran='+Math.random(),
								success:function(){
									easyDialog.open({
										container:{
											header:'温馨提示',
											content:"提交成功",
											yesFn:function(){
												toBack();
											  }
										}
									});
								},
								error:function(data){
									easyDialog.open({
										container:{
											header:'温馨提示',
											content:data.msg
										},
										autoClose : 2000
									});
								}
							});
						}
						
					}else{
						if(Id[i]==$("#countyId").val()){
							//alert("该区域已经存在！请重新输入");
							easyDialog.open({container:{header:'温馨提示',content:"该区域已经存在！请重新输入"},autoClose : 2000});
							return;
						}else{
							$.ajax({
								type:"get",
								data:{
									id:id,
									areaId:Id[i]
								},
								url:server_url+'/rest/supplyManage/updateById.do?token='+token+'&ran='+Math.random(),
								success:function(){
									easyDialog.open({
										container:{
											header:'温馨提示',
											content:"提交成功",
											yesFn:function(){
												toBack();
											  }
										}
									});
								},
								error:function(data){
									easyDialog.open({
										container:{
											header:'温馨提示',
											content:data.msg
										},
										autoClose : 2000
									});
								}
							});
						}
						
					}
				}
			}
		}
	});
};
